package com.cheyunkeji.er.activity.evaluate;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.evaluate.BCSMCheckItemView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class AppearanceCheckBCSM extends BaseActivity {
    private static final String TAG = AppearanceCheckBCSM.class.getSimpleName() ;
    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.bcsm_10)
    BCSMCheckItemView bcsm10;
    @BindView(R.id.bcsm_11)
    BCSMCheckItemView bcsm11;
    @BindView(R.id.bcsm_12)
    BCSMCheckItemView bcsm12;
    @BindView(R.id.bcsm_13)
    BCSMCheckItemView bcsm13;
    @BindView(R.id.bcsm_14)
    BCSMCheckItemView bcsm14;
    @BindView(R.id.bcsm_15)
    BCSMCheckItemView bcsm15;
    @BindView(R.id.bcsm_16)
    BCSMCheckItemView bcsm16;
    @BindView(R.id.bcsm_17)
    BCSMCheckItemView bcsm17;
    @BindView(R.id.bcsm_18)
    BCSMCheckItemView bcsm18;
    @BindView(R.id.bcsm_19)
    BCSMCheckItemView bcsm19;
    @BindView(R.id.bcsm_20)
    BCSMCheckItemView bcsm20;
    @BindView(R.id.bcsm_21)
    BCSMCheckItemView bcsm21;
    @BindView(R.id.bcsm_22)
    BCSMCheckItemView bcsm22;
    @BindView(R.id.bcsm_23)
    BCSMCheckItemView bcsm23;
    @BindView(R.id.bcsm_24)
    BCSMCheckItemView bcsm24;
    @BindView(R.id.bcsm_25)
    BCSMCheckItemView bcsm25;
    @BindView(R.id.activity_appearance_check_bcsm)
    LinearLayout activityAppearanceCheckBcsm;

    private Map<Integer, BCSMCheckItemView> itemViewList = null;

    private List<EvaluateDetailResult2.DetectionBean.ApshellMarkBean> apshellMarkBeanList = null;

    @Override
    protected void init() {
        setContentView(R.layout.er_activity_appearance_check_bcsm);
        ButterKnife.bind(this);

        itemViewList = new HashMap<>();
        itemViewList.put(10,bcsm10);
        itemViewList.put(11,bcsm11);
        itemViewList.put(12,bcsm12);
        itemViewList.put(13,bcsm13);
        itemViewList.put(14,bcsm14);
        itemViewList.put(15,bcsm15);
        itemViewList.put(16,bcsm16);
        itemViewList.put(17,bcsm17);
        itemViewList.put(18,bcsm18);
        itemViewList.put(19,bcsm19);
        itemViewList.put(20,bcsm20);
        itemViewList.put(21,bcsm21);
        itemViewList.put(22,bcsm22);
        itemViewList.put(23,bcsm23);
        itemViewList.put(24,bcsm24);
        itemViewList.put(25,bcsm25);
    }

    @Override
    protected void initView() {
        vTopbar.setLeftBack();
        vTopbar.setRight("保存", -1, this);
        vTopbar.setTitle("外观与车体补充说明");
        if (null != MyApplication.getInstance().getEvaluateResult() && null != MyApplication.getInstance().getEvaluateResult().getDetection() && null != MyApplication.getInstance().getEvaluateResult().getDetection().getApshell_mark()) {
            apshellMarkBeanList = MyApplication.getInstance().getEvaluateResult().getDetection().getApshell_mark();
            if (apshellMarkBeanList.size() > 0) {
                displayInfo();
            }
        }
    }

    private void displayInfo() {
        for (EvaluateDetailResult2.DetectionBean.ApshellMarkBean bean : apshellMarkBeanList) {
            itemViewList.get(bean.getId()).setCheckedIndex(bean.getStatus()-1);
            itemViewList.get(bean.getId()).setTag(bean);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(apshellMarkBeanList == null){
            apshellMarkBeanList = new ArrayList<>();
        }else{
            apshellMarkBeanList.clear();
        }
        if(v.getId() == R.id.vRight){
            for(int  index : itemViewList.keySet() ){
                if(3 != itemViewList.get(index).getCurrentCheckedIndex()){
                    apshellMarkBeanList.add(new EvaluateDetailResult2.DetectionBean.ApshellMarkBean(index,itemViewList.get(index).getCurrentCheckedIndex() + 1));
                }
            }
            String myData = new StringBuilder("{\"apshell_mark\":").append(new Gson().toJson(apshellMarkBeanList)).append("}").toString();
            Log.e(TAG, "onClick: " + new Gson().toJson(apshellMarkBeanList));
            HashMap<String, String> params = new HashMap<>();
//            params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
//            params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(), ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
            params.put("aid",MyApplication.getInstance().getEvaluateResult().getAid());
            params.put("detection",myData);
            ApiClient.postForm(HttpConstants.INSERT_ARCHIVE_INFO, params, new RespCallback<EvaluateDetailResult2>() {

                @Override
                protected void onRequestFailed(String msg) {
                    SToast.show(msg);
                }

                @Override
                protected void onSuccess(EvaluateDetailResult2 data) {
                    if (null != data) {
                        MyApplication.getInstance().setEvaluateResult(data);
                        SToast.show("保存成功");
                        MyApplication.getInstance().removeActivity(AppearanceCheckBCSM.class);
                        finish();
                    }
                }
                @Override
                public void onBefore(Request request, int id) {
                    showDialog();
                }

                @Override
                public void onAfter(int id) {
                    dismissDialog();
                }

            });

        }
    }
}
